<template>
  <div>
    <h2 v-text="questionText"></h2>
    <button @click="handleBatchRequest">Send Batch Requests</button>
    <div v-if="results.length">
      <h3>结果:</h3>
      <ul>
        <li v-for="(result, index) in results" :key="index">{{ result }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
// import axios from '@/mock/mockService'; 

export default {
  data() {
    return {
      results: [],
      questionText: '第二题  异步请求处理： 使用 JavaScript 实现一个异步函数 batchRequest(urls: string[], batchSize: number): Promise<any[]>，该函数接收一个 URL 数组和一个批次大小参数，返回一个包含所有请求结果的数组。每个批次最多同时执行 batchSize 个请求，直到所有请求完成。'
    };
  },
  methods: {
    async handleBatchRequest() {
      const urls = [
        'https://api.example.com/data1',
        'https://api.example.com/data2',
        'https://api.example.com/data3',
        'https://api.example.com/data4',
        'https://api.example.com/data5',
        'https://api.example.com/data6',
        'https://api.example.com/data7',

      ];
      const batchSize = 2;
      try {
        const results = await this.batchRequest(urls, batchSize);
        this.results = results.map(result => result.data);
      } catch (error) {
        console.error(error);
      }
    },
    async batchRequest(urls, batchSize) {
      console.log(urls, batchSize);
      // 请考生在这里编写
    }
  }
};
</script>
